package com.gaogzhen.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaogzhen.domain.CoinType;
import com.gaogzhen.model.ExceptionEnum;
import com.gaogzhen.model.RRException;
import com.gaogzhen.service.CoinTypeService;
import com.gaogzhen.utils.R;
import com.gaogzhen.utils.SqlExecuteUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 * @description 币种类型控制器
 * @date 2022-11-04 10:14
 */
@RestController
@RequestMapping("/coinTypes")
@Api(tags = "币种类型控制器")
public class CoinTypeController {
    @Autowired
    private CoinTypeService coinTypeService;

    @GetMapping
    @ApiOperation("分页查询币种类型列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页大小"),
            @ApiImplicitParam(name = "code", value = "币种类型"),
    })
    @PreAuthorize("hasAuthority('trade_coin_type_query')")
    public R<Page<CoinType>> findByPage(@ApiIgnore Page<CoinType> page, String code) {
        return R.ok(coinTypeService.findByPage(page, code));

    }

    @PostMapping
    @ApiOperation("新增币种类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coinType", value = "coinType json数据")
    })
    @PreAuthorize("hasAuthority('trade_coin_type_create')")
    public R add(@RequestBody @Validated CoinType coinType) {
        SqlExecuteUtils.sqlExceptionHandler(a ->coinTypeService.save(a), coinType, ExceptionEnum.COIN_TYPE_ADD_FAIL);

        return R.ok();
    }

    @PatchMapping
    @ApiOperation("修改币种类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coinType", value = "coinType json数据")
    })
    @PreAuthorize("hasAuthority('trade_coin_type_update')")
    public R update(@RequestBody CoinType coinType) {
        SqlExecuteUtils.sqlExceptionHandler(a ->coinTypeService.updateById(a), coinType, ExceptionEnum.COIN_TYPE_UPDATE_FAIL);

        return R.ok();
    }

    @PostMapping("/setStatus")
    @ApiOperation("设置币种类型状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "coinType唯一标识"),
            @ApiImplicitParam(name = "status", value = "coinType的状态"),
    })
    @PreAuthorize("hasAuthority('trade_coin_type_update')")
    public R setStatus(@RequestParam(required = true) Long id, @RequestParam(required = true) Byte status) {
        coinTypeService.setStatus(id, status);
        return R.ok();
    }

    @GetMapping("/all")
    @ApiOperation("查询所有的币种类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "状态")
    })
    @PreAuthorize("hasAuthority('trade_coin_type_query')")
    public R<List<CoinType>> findAll(@RequestParam(required = true) Byte status) {
        return R.ok(coinTypeService.findAll(status));
    }

}
